package com.ztc.ZtcClassesSiteForSale.dao;
import com.aqgj.util.BaseDaoImpl;
import com.ztc.ZtcClassesSiteForSale.bean.ZtcClassesSiteForSale;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;
/**
 * <p>
 * 
 * </p>
 * 
 * @ClassName ZtcClassesSiteForSaleDao
 * @JDK 1.5
 * @author 
 * @date  
 */
@Repository("ZtcClassesSiteForSaleDao")
public class ZtcClassesSiteForSaleDao extends BaseDaoImpl<ZtcClassesSiteForSale> {
	private static final SimpleDateFormat DATE_TIME_FORMET = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	public String save(ZtcClassesSiteForSale ztcClassesSiteForSale) {
		this.getHibernateTemplate().save(ztcClassesSiteForSale);
		return ztcClassesSiteForSale.getId();
	}
	public List<ZtcClassesSiteForSale> getAllZtcClassesSiteForSales() {
		return this.getAll();
	}

	public String delete(String id, String ztcClassesForSaleId,String modifyUserId) {
		String sql = "delete from ztc_classes_site_for_sale where 1=1";
		if (id != null) {
			sql += " and id='" + id + "'";
		}
		if (ztcClassesForSaleId != null) {
			sql += " and fk_ztc_classes_for_sale_id='" + ztcClassesForSaleId + "'";
		}
		if(modifyUserId!=null){
			sql += " and fk_modify_user_id='" + modifyUserId + "'";
		}
		if (this.currentSession().createNativeQuery(sql).executeUpdate() >= 0) {
			return "1";
		}
		return "0";
	}

	public List<ZtcClassesSiteForSale> searchInfoEquals(ZtcClassesSiteForSale ztcClassesSiteForSale) {
		String sql = "select * from ztc_classes_site_for_sale where 1=1";
		if (ztcClassesSiteForSale.getName() != null) {
			sql += " and name='" + ztcClassesSiteForSale.getName() + "'";
		}
		if (ztcClassesSiteForSale.getAreaCity() != null) {
			sql += " and area_city='" + ztcClassesSiteForSale.getAreaCity() + "'";
		}
		if (ztcClassesSiteForSale.getAreaCode() != null) {
			sql += " and area_code='" + ztcClassesSiteForSale.getAreaCode() + "'";
		}
		if (ztcClassesSiteForSale.getManagerPhone() != null) {
			sql += " and manager_phone='" + ztcClassesSiteForSale.getManagerPhone() + "'";
		}
		if (ztcClassesSiteForSale.getStatus() != null) {
			sql += " and status='" + ztcClassesSiteForSale.getStatus() + "'";
		}
		if (ztcClassesSiteForSale.getSequence() != null) {
			sql += " and sequence='" + ztcClassesSiteForSale.getSequence() + "'";
		}
		if (ztcClassesSiteForSale.getLat() != null) {
			sql += " and lat='" + ztcClassesSiteForSale.getLat()+ "'";
		}
		if (ztcClassesSiteForSale.getDepartureTime() != null) {
			sql += " and departure_time='" + DATE_TIME_FORMET.format(ztcClassesSiteForSale.getDepartureTime()) + "'";
		}
		if (ztcClassesSiteForSale.getFkZtcClassesForSaleId() != null) {
			sql += " and fk_ztc_classes_for_sale_id='" + ztcClassesSiteForSale.getFkZtcClassesForSaleId() + "'";
		}
		if (ztcClassesSiteForSale.getId() != null) {
			sql += " and id='" + ztcClassesSiteForSale.getId() + "'";
		}
		sql += " order by sequence";
		List<ZtcClassesSiteForSale> list = this.currentSession().createNativeQuery(sql,ZtcClassesSiteForSale.class).getResultList();
		return list;
	}
	

	public List<ZtcClassesSiteForSale> searchInfoLikes(String name, String areaCity,String managerPhone) {
		String sql = "select * from ztc_classes_site_for_sale where 1=1";
		if (name != null) {
			sql += " and name like '%" + name + "%'";
		}
		if (managerPhone != null) {
			sql += " and manager_phone like '%" + managerPhone + "%'";
		}
		if (areaCity != null) {
			sql += " and area_city like '%" + areaCity + "%'";
		}
		List<ZtcClassesSiteForSale> list = this.currentSession().createNativeQuery(sql,ZtcClassesSiteForSale.class).getResultList();
		return list;
	}

	public String update(ZtcClassesSiteForSale ztcClassesSiteForSale) {
		this.getHibernateTemplate().saveOrUpdate(ztcClassesSiteForSale);
		if (ztcClassesSiteForSale.getId() != null) {
			return "1";
		}
		return "0";
	}

	public List<ZtcClassesSiteForSale> searchOverdueSiteForSale() {
		String sql = "select * from ztc_classes_site_for_sale where ztc_classes_site_for_sale.type!=2 and ztc_classes_site_for_sale.departure_time<=NOW() and ztc_classes_site_for_sale.status='0' order by ztc_classes_site_for_sale.departure_time";
		List<ZtcClassesSiteForSale> list = this.currentSession().createNativeQuery(sql,ZtcClassesSiteForSale.class).getResultList();
		return list;
	}
}